package com.base.andlangcore.http.interceptor

import android.text.TextUtils
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response

/**
 *  拦截后 设置请求 token
 **/
class TokenInterceptor : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
        val originalRequest = chain.request()
        var tokenRequest: Request? = null

        var token = ""

        if (TextUtils.isEmpty(token)) {
            return chain.proceed(originalRequest)
        }

        val headerValues = originalRequest.headers("BaseUrl")
        if (headerValues.isNotEmpty()) {
            val headToken = headerValues[0]
            tokenRequest = when (headToken) {
                "otherHost" -> {
                    originalRequest.newBuilder()
                        .header("Authorization", token)
                        .build()
                }
                else -> {
                    originalRequest.newBuilder()
                        .header("Authorization", token)
                        .build()
                }
            }
        } else {
            tokenRequest = originalRequest.newBuilder()
                .header("Authorization", token)
                .build()
        }
        return chain.proceed(tokenRequest)
    }

}